﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveController : MonoBehaviour {


    public float moveSpeedRatio = 1.3f;
    public float rotateSpeedRatio = 1.5f;
    private CharacterController characterController;
    private Transform rotateAxis;
    private Transform PlayerTransform;

    public void Init(Transform PlayerTransform,RoleController roleController)
    {
        this.PlayerTransform = PlayerTransform;
        rotateAxis = PlayerTransform.Find("RotateAxis");
        characterController = PlayerTransform.GetComponent<CharacterController>();
        if (roleController.info.isLocalPlayer)
        {
            //给手柄挂在监听事件
            ETCJoyUIController._instance.leftJoyStick.onMove.AddListener(TouchOnMove);
            ETCJoyUIController._instance.leftJoyStick.cameraLookAt = rotateAxis;
        }
        else
        {
            //移动动画函数挂在到getter事件上
        }
    }


    /// <summary>
    /// 移动
    /// </summary>
    /// <param name="pos"></param>
    public void TouchOnMove(Vector2 pos)
    {
        float JD_Tan = Mathf.Abs(pos.y / pos.x);
        // 当pos.y/pos.x 的绝对值大于等0，小于等于0.3时(0.3代表一个小于tan30°的某个角度)，是左右平移，否则前后移动加旋转
        if (JD_Tan < 0.3)
        {
            print("前进");
            MoveForward(PlayerTransform.right, pos.x);
        }
        else
        {
            MoveForward(PlayerTransform.forward, pos.y);
            RotateDir(pos.x);
        }
    }

    /// <summary>
    /// 前进
    /// </summary>
    /// <param name="dir"></param>
    /// <param name="moveSpeed"></param>
    private void MoveForward(Vector3 dir, float moveSpeed)
    {
        characterController.SimpleMove(dir * moveSpeed * moveSpeedRatio);
    }

    /// <summary>
    /// 旋转
    /// </summary>
    private void RotateDir(float rotateSpeed)
    {
        //PlayerTransform.Rotate(Vector3.up * rotateSpeed* rotateSpeedRatio);
        PlayerTransform.RotateAround(rotateAxis.position, Vector3.up, rotateSpeed * rotateSpeedRatio);
    }
}
